package com.kuang.controller;

import com.kuang.pojo.Readers;
import com.kuang.pojo.Users;
import com.kuang.service.UserService;
import com.kuang.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //验证
    @RequestMapping("/yz")
    public String yz(String Name,String Pass){
        String b=EncryptUtil.md5(Pass);
        Boolean a = userService.queryNP(Name,b);
        if (a==null){
            return "user/login";
        }
        return "index/index";
    }

    @RequestMapping("/updatePass")
    public String xg(){
        return "user/updatePass";
    }

    @RequestMapping("/updatepass")
    public String updatepass(String userName,String userPass,String userPassNew){
        String old = EncryptUtil.md5(userPass);
        String now = EncryptUtil.md5(userPassNew);
        userService.updatepass(userName,old,now);
        return "user/login";
    }

    @RequestMapping("/insertUser")
    public String zc(){
        return "user/insertUser";
    }

    @RequestMapping("/insertpass")
    public String insertpass(String userName,String userPass){
        String pass=EncryptUtil.md5(userPass);
        userService.insertpass(userName,pass);
        return "user/login";
    }

@RequestMapping("/allUser")
    private String list(Model model){
        List<Users> list = userService.alluser();
    System.out.println(list);
        model.addAttribute("list",list);
        return "user/allUser";
}

@RequestMapping("/back")
    private String back(){
    return "index/index";
}

@RequestMapping("/deleteuser/{userID}")
    public String deleteuser(@PathVariable("userID") int id){
        userService.deleteById(id);
    return "redirect:/user/allUser";
}
}
